<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Redirect, Input;
use App\Landowner;
use Illuminate\Support\Facades\DB;

class landOwnerController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    //
    public function index()  
	{
	  return view('landowner.index')->withLands(Landowner::all());
	}

    public function all()  
    {
        return response()->json(Landowner::all()); 
    }

	public function create()
    {
        return view('landowner.create');
    }

    public function show($id)
    {
        $landowner = DB::table('landowners')->where('id', $id)->first();
        return view('landowner.detail')->withLandowner($landowner);
            // return response()->json($landowner); 
    }

    public function autocomplete()
    {
        $landowner = DB::select('select name from landowners order by name asc');
        return response()->json($landowner); 
    }


    public function modify($id)
    {
        $landowner = DB::table('landowners')->where('id', $id)->first();
    
        return view('landowner.modify')->withLandowner($landowner);
        // return response()->json($landowner); 
    }

    public function store(Request $request)
    {
               // 'idcardNo' => 'required|unique:landowners|idcards',
      
        $this->validate($request, [
           'name' => 'required',
           'idcardNo' => 'required'
        ]);

        $land = new Landowner;
        $land->name = $request->input("name");
        $land->team = $request->input("team");
        $land->idcardNo = $request->input("idcardNo");
        $land->address = $request->input("address");
 		$land->sex = $request->input("sex");
		$land->nation = $request->input("nation");
		$land->cardPic = $request->input("cardPic");
	
        if ($land->save()) {
            return Redirect::to('landowner');
        } else {
            return Redirect::back()->withInput()->withErrors('保存失败！');
        }

    }

    public function update(Request $request)
    {
            // 'idcardNo' => 'required|idcards',
        $this->validate($request, [
            'id' => 'required',
            'name' => 'required',
        ]);

   
        $landowner = ["name"=>$request->input("name"),
        "team"=>$request->input("team"),
        "idcardNo"=>$request->input("idcardNo"),
        "address"=>$request->input("address"),
        "sex"=>$request->input("sex"),
        "nation"=>$request->input("nation"),
        "cardPic"=>$request->input("cardPic")
        ];
    
        DB::table('landowners')
            ->where('id', $request->input("id"))
            ->update($landowner);
      
        return Redirect::to('landowner');
    }

    public function delete($id)
    {
        $count =  DB::table('contracts')->where("landowner_id", $id)->count();
        if ($count > 0){
              return Redirect::back()->withInput()->withErrors('该人下有合同，不能删除！');
        }

        DB::table('landowners')->where('id', $id)->delete();
     
     // return response()->json($id); 
        return Redirect::to('landowner');
     }
}
